<?php

use Doctrine\ORM\EntityManager,
    Doctrine\ORM\Configuration;
    
   set_time_limit(500);
$raizConfig = dirname(__FILE__) . "/..";
require dirname(__FILE__) . "/../set_AplicationEnv.php";
require ROOTFOLDER . "/Doctrine/Common/ClassLoader.php";


/*--------------- Class Loaders por namespaces-----------------*/
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine',"$raizConfig/");
$classLoader->register(); // register on SPL autoload stack

$classLoader = new \Doctrine\Common\ClassLoader('Symfony', "$raizConfig/Doctrine");
$classLoader->register();

$classLoader2 = new \Doctrine\Common\ClassLoader('Entities', "$raizConfig/");
$classLoader2->register(); // register on SPL autoload stack

$classLoader2 = new \Doctrine\Common\ClassLoader('Business', "$raizConfig/");
$classLoader2->register(); // register on SPL autoload stack

/*--------------------- Configuracao do cach ------------------*/
//if(APPLICATION_ENV == "development")
 $cache = new \Doctrine\Common\Cache\ArrayCache;
//else if(APPLICATION_ENV == "production")
// $cache = new \Doctrine\Common\Cache\ApcCache();

/*-------------------------------------------------------------*/
 
 $config = new Configuration();
 $config->setMetadataCacheImpl($cache);
 $driverImpl = $config->newDefaultAnnotationDriver('./Entities');
 $config->setMetadataDriverImpl($driverImpl);
 $config->setQueryCacheImpl($cache);
 $config->setProxyDir($raizConfig .'/Entities/Proxies');
 $config->setProxyNamespace('MyProject\Proxies');
 $config->setAutoGenerateProxyClasses(true);
 
if(APPLICATION_ENV == "development"){
	$conn = array(
	    'dbname' => 'dbju',
	    'user' => 'root',
	    'password' => '123',
	    'host' => 'localhost',
	    'driver' => 'pdo_mysql',
	);
}
else if(APPLICATION_ENV == "production"){
	$conn = array(
	    'dbname' => 'dbju',
	    'user' => 'root',
	    'password' => '123',
	    'host' => 'localhost',
	    'driver' => 'pdo_mysql',
	);
}
global $em;
$em = EntityManager::create($conn, $config);

function getEm(){
global $em;
	return $em;
}

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));

require_once ROOTFOLDER . "/includes/utils.php";

$connect = mysql_connect("localhost", "root", "123");
mysql_select_db("dbadmsys");
$query = "select * from cidades";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
	
	$cidade = new Entities\Cidade();
	$cidade->setNome($row["nomeMunicipio"]);
	$q = $em->createQuery("select e from Entities\Estado e where e.sigla = '".$row["SUF"]."'");
	$estados = $q->getResult();
	foreach ($estados as $estado) {
		$cidade->setEstado($estado);
	}
	echo $cidade->getEstado()->getSigla();
	$em->persist($cidade);
	
}
$em->flush();

?>